#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
环境检查脚本
验证tickt-boost conda环境是否正确配置
"""

import sys
import os
import subprocess
from importlib import import_module

print("🔍 Ticket-Boost 环境检查")
print("=" * 50)

# 检查Python版本
print(f"Python版本: {sys.version}")
print(f"Python路径: {sys.executable}")

# 检查conda环境
try:
    result = subprocess.run(["conda", "info", "--envs"], capture_output=True, text=True)
    if "tickt-boost" in result.stdout:
        print("✅ tickt-boost conda环境存在")
    else:
        print("❌ tickt-boost conda环境不存在")
except:
    print("⚠️  无法检查conda环境")

print("\n📦 依赖包检查:")

# 必需的核心包
required_packages = {
    "airtest": "Airtest移动自动化框架",
    "opencv": "OpenCV图像处理",
    "selenium": "Web自动化",
    "uiautomator2": "Android自动化",
    "requests": "HTTP请求",
    "pandas": "数据处理",
    "PIL": "图像处理 (Pillow)",
    "cryptography": "加密功能",
    "yaml": "YAML配置文件",
    "plyer": "通知功能",
}

missing_packages = []
installed_packages = []

for package, description in required_packages.items():
    try:
        if package == "opencv":
            import cv2

            installed_packages.append(f"  ✅ {package}: {description}")
        elif package == "PIL":
            from PIL import Image

            installed_packages.append(f"  ✅ {package}: {description}")
        elif package == "yaml":
            import yaml

            installed_packages.append(f"  ✅ {package}: {description}")
        else:
            import_module(package)
            installed_packages.append(f"  ✅ {package}: {description}")
    except ImportError:
        missing_packages.append(f"  ❌ {package}: {description}")

# 输出结果
for pkg in installed_packages:
    print(pkg)

if missing_packages:
    print("\n⚠️  缺失的包:")
    for pkg in missing_packages:
        print(pkg)
else:
    print("\n🎉 所有依赖包已正确安装!")

# 检查设备连接
print(f"\n🔧 系统检查:")
try:
    from utils.airtest_driver import AIRTEST_AVAILABLE

    if AIRTEST_AVAILABLE:
        print("  ✅ Airtest框架可用")
    else:
        print("  ⚠️  Airtest框架不可用")
except ImportError:
    print("  ❌ 无法导入Airtest驱动器")

# 检查配置文件
if os.path.exists("config/config.json"):
    print("  ✅ 配置文件存在")
else:
    print("  ⚠️  配置文件不存在，请运行 python main.py --config")

# 检查ADB
try:
    result = subprocess.run(["adb", "version"], capture_output=True, text=True)
    if result.returncode == 0:
        print("  ✅ ADB可用")
    else:
        print("  ⚠️  ADB不可用")
except:
    print("  ⚠️  ADB不可用")

print(f"\n{'='*50}")
if not missing_packages:
    print("🚀 环境配置完成！可以开始使用系统。")
    print("\n推荐命令:")
    print("  python main.py --test      # 测试设备连接")
    print("  python main.py --config    # 配置参数")
    print("  python main.py             # 开始抢票")
else:
    print("⚠️  环境配置不完整，请安装缺失的依赖包。")
    print("运行: pip install -r requirements.txt")
